package com.example.rbacmanage.controller.background;

import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.app.MessageBoard;
import com.example.rbacmanage.service.app.MessageBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/background/messageboard")
public class MessageBoardController {
    @Autowired
    private MessageBoardService messageBoardService;

    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('background:messageboard:list')")
    public Result page(MessageBoard messageBoard){
        return Result.success(messageBoardService.getPage(messageBoard));
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Long id){
        return Result.success(messageBoardService.getById(id));
    }


    @PreAuthorize("hasAnyAuthority('background:messageboard:add')")
    @PostMapping("/add")
    public Result add(@RequestBody MessageBoard messageBoard){
        messageBoard.setCreateTime(new Date());
        messageBoardService.save(messageBoard);
        return Result.success();
    }
    @PreAuthorize("hasAnyAuthority('background:messageboard:update')")
    @PutMapping("/update")
    public Result update(@RequestBody MessageBoard messageBoard){
        messageBoardService.update(messageBoard);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('background:messageboard:remove')")
    @DeleteMapping("/remove")
    public Result remove(@RequestBody Long[] ids){
        messageBoardService.remove(ids);
        return Result.success();
    }
}
